package com.young.expression;

import java.text.ParseException;
import java.util.Map;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class SpelExpressionEvaluator implements ExpressionEvaluator{

	private ExpressionParser parser = new SpelExpressionParser();
	
	@Override
	public Object evaluator(String script, Map<String, Object> context)
			throws ParseException, EvaluationException {
		Expression expression = parser.parseExpression(script);
		EvaluationContext evalContext = new StandardEvaluationContext();
		for(String key : context.keySet()){
			evalContext.setVariable(key, context.get(key));
		}
		return expression.getValue(evalContext);
	}

}
